package com.gopay.bis.manager.bank.Wap;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.tbank.ebank.pay.TBankMessage;
import com.gopay.common.domain.bank.tbank.ebank.query.req.TBankOrderReq;
import com.gopay.common.domain.bank.tbank.ebank.query.res.TBankOrderRes;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.WapPayManagerRemoteService;
import com.gopay.remote.bank.businesses.BankService;
import org.apache.commons.lang.StringUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

//import com.gopay.remote.bank.businesses.WapPayService;

/**
 * 测试银行B2C对象封装
 * 
 * @TBankB2CManager.java
 * @author WangHongFei
 */
public class TBankWapB2cManager implements WapPayManagerRemoteService {

    @Resource(name = "tbankConfig")
    private Map<String, String> tbankConfig;

    @Resource(name = "tbankB2cBankServiceClient")
    private BankService tbankB2cBankServiceClient;

    /**
     * 测试银行签名
     *
     * @param BaseMessage
     * @return Object
     * @throws com.gopay.common.exception.GopayException
     */
    public Object sign(BaseMessage message) throws GopayException {
		TBankMessage messageB2C = (TBankMessage) message;
    	messageB2C.setMsgType(tbankConfig.get("TBANK.B2C.MSGTYPE"));
    	messageB2C.setTxnType(tbankConfig.get("TBANK.B2C.TXNTYPE"));
    	messageB2C.setMercNo(tbankConfig.get("TBANK.B2C.MerchantNo"));
    	messageB2C.setOrderUrl(tbankConfig.get("TBANK.B2C.OrderUrl"));
    	messageB2C.setOrderSerUrl(tbankConfig.get("TBANK.B2C.OrderSerUrl"));
    	messageB2C.setBankUrl(tbankConfig.get("TBANK.B2C.BankUrl"));
    	messageB2C.setCurCode(tbankConfig.get("TBANK.CUR.CODE"));
    	messageB2C.setOrderDate("20121127");
    	messageB2C.setOrderTime("020610");
    	messageB2C.setPlainMessage(buildMessage(messageB2C));
    	messageB2C.setSignMessage(messageB2C.getPlainMessage());
    	BankLogUtils.printB2CManagerSignSendLog(messageB2C);
    	String signStr = (String) tbankB2cBankServiceClient.sign(messageB2C);
    	messageB2C.setSignMessage(signStr);
    	BankLogUtils.printB2CManagerSignReturnLog(messageB2C);
        return messageB2C;

    }

    /**
     * 测试银行验签
     *
     * @param BaseMessage
     * @return Object
     * @throws com.gopay.common.exception.GopayException
     */
    public Object verify(BaseMessage message) throws GopayException {
    	TBankMessage messageB2C = (TBankMessage) message;
	    messageB2C.setMsgType(tbankConfig.get("TBANK.B2C.MSGTYPE"));
    	messageB2C.setTxnType(tbankConfig.get("TBANK.B2C.TXNTYPE"));
    	messageB2C.setMercNo(tbankConfig.get("TBANK.B2C.MerchantNo"));
    	messageB2C.setOrderUrl(tbankConfig.get("TBANK.B2C.OrderUrl"));
    	messageB2C.setOrderSerUrl(tbankConfig.get("TBANK.B2C.OrderSerUrl"));
    	messageB2C.setBankUrl(tbankConfig.get("TBANK.B2C.BankUrl"));
    	messageB2C.setCurCode(tbankConfig.get("TBANK.CUR.CODE"));
    	messageB2C.setOrderDate("20121127");
    	messageB2C.setOrderTime("020610");
    	messageB2C.setPlainMessage(buildMessage(messageB2C));
    	messageB2C.setSignMessage(messageB2C.getPlainMessage());
    	BankLogUtils.printB2CManagerVerifySendLog(messageB2C);
    	boolean flag = (Boolean) tbankB2cBankServiceClient.verify(messageB2C);
    	BankLogUtils.printB2CManagerVerifyReturnLog(messageB2C, flag);
    	return flag;
    }

    /**
     * 测试银行订单查询
     *
     * @param Object
     * @return Object
     * @throws com.gopay.common.exception.GopayException
     */
    public Object query(Object message) throws GopayException {
		TBankOrderReq messageB2C = (TBankOrderReq) message;
    	messageB2C.setOrderNo(messageB2C.getOrderId());
    	messageB2C.setMercNo(tbankConfig.get("TBANK.B2C.MerchantNo"));
    	messageB2C.setMsgType(tbankConfig.get("TBANK.B2C.QUERY.MSGTYPE"));
    	messageB2C.setTxnType(tbankConfig.get("TBANK.B2C.TXNTYPE"));
    	messageB2C.setBankUrl(tbankConfig.get("TBANK.B2C.BankUrl"));
    	messageB2C.setSignDate(this.getCurrDate());
    	BankLogUtils.printB2CManagerQuerySendLog(messageB2C, "");
    	TBankOrderRes resMessage = (TBankOrderRes) tbankB2cBankServiceClient.query(messageB2C);
    	resMessage.setOutStlmCd(BankConstants.BANK_LOGO_TBANK); // 为了区分日志输出
    	BankLogUtils.printB2CManagerQueryReturnLog(resMessage);
	    return resMessage;
    }
    
    /**
     * 构建明文信息
     * 
     * @param TBankMessage
     * @return String
     */
    private String buildMessage(TBankMessage messageB2C){
        StringBuffer sb = new StringBuffer(512);
        sb.append(StringUtils.trimToEmpty(messageB2C.getOrderId())).append("|");
        sb.append(StringUtils.trimToEmpty(messageB2C.getOrderTime())).append("|");
        sb.append(StringUtils.trimToEmpty(messageB2C.getCurCode())).append("|");
        sb.append(StringUtils.trimToEmpty(messageB2C.getAmount().toString())).append("|");
        sb.append(StringUtils.trimToEmpty(messageB2C.getMercNo()));
        return sb.toString();
    }
    
    /**
	 * 获取当前时间
	 * @return String
	 */
	private String getCurrDate(){
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String s = sdf.format(date);
		return s;
		
	}

	@Override
	public Object dayend(BaseMessage base) throws GopayException, Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
